package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.WmsInPlan;
import com.ruoyi.business.domain.WmsInWarehous;
import com.ruoyi.business.domain.WmsTransfer;
import com.ruoyi.business.domain.WmsTransferRecord;
import com.ruoyi.business.mapper.WmsInPlanMapper;
import com.ruoyi.business.mapper.WmsInWarehousMapper;
import com.ruoyi.business.mapper.WmsTransferMapper;
import com.ruoyi.business.mapper.WmsTransferRecordMapper;
import com.ruoyi.business.service.IWmsInPlanService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 入库计划Service业务层处理
 * 
 * @author sz
 * @date 2024-01-08
 */
@Service
public class WmsInPlanServiceImpl implements IWmsInPlanService
{
    @Autowired
    private WmsInPlanMapper wmsInPlanMapper;
    @Autowired
    private WmsInWarehousMapper wmsInWarehousMapper;
    @Autowired
    private WmsTransferMapper wmsTransferMapper;

    /**
     * 查询入库计划
     * 
     * @param id 入库计划ID
     * @return 入库计划
     */
    @Override
    public WmsInPlan selectWmsInPlanById(Long id)
    {
        return wmsInPlanMapper.selectWmsInPlanById(id);
    }

    @Override
    public WmsInPlan selectWmsInPlanBySn(String sn) {
        return wmsInPlanMapper.selectWmsInPlanBySn(sn);
    }

    /**
     * 查询入库计划列表
     * 
     * @param wmsInPlan 入库计划
     * @return 入库计划
     */
    @Override
    public List<WmsInPlan> selectWmsInPlanList(WmsInPlan wmsInPlan)
    {
        return wmsInPlanMapper.selectWmsInPlanList(wmsInPlan);
    }

    /**
     * 新增入库计划
     * 
     * @param wmsInPlan 入库计划
     * @return 结果
     */
    @Override
    public int insertWmsInPlan(WmsInPlan wmsInPlan)
    {
        wmsInPlan.setCreateTime(DateUtils.getNowDate());
        wmsInPlanMapper.insertWmsInPlan(wmsInPlan);

        //保存入库单记录
        WmsInWarehous wmsInWarehous = new WmsInWarehous();
        wmsInWarehous.setInPlanId(wmsInPlan.getId());
        wmsInWarehous.setStatus(1);
        wmsInWarehous.setInStoreSn(WebUtil.createCashOutNum("RK"));
        wmsInWarehous.setCreateBy(wmsInPlan.getCreateBy());
        wmsInWarehous.setCreateTime(new Date());
        wmsInWarehous.setRemark(wmsInPlan.getRemark());
        wmsInWarehousMapper.insertWmsInWarehous(wmsInWarehous);

        //判断入库计划是否选择调拨单
        if(StringUtils.isNotEmpty(wmsInPlan.getTransferSn())){
            Map<String,Object> params = new HashMap<>();
            params.put("transferSn",wmsInPlan.getTransferSn());
            params.put("inStatus",2);
            wmsTransferMapper.updateInStatus(params);
        }
        return 1;
    }

    /**
     * 修改入库计划
     * 
     * @param wmsInPlan 入库计划
     * @return 结果
     */
    @Override
    public int updateWmsInPlan(WmsInPlan wmsInPlan)
    {
        WmsInPlan old = wmsInPlanMapper.selectWmsInPlanById(wmsInPlan.getId());

        wmsInPlan.setUpdateTime(DateUtils.getNowDate());
        wmsInPlanMapper.updateWmsInPlan(wmsInPlan);

        if(StringUtils.isNotEmpty(wmsInPlan.getTransferSn())
                && StringUtils.isNotEmpty(old.getTransferSn())
                && !wmsInPlan.getTransferSn().equals(old.getTransferSn())){
            Map<String,Object> params = new HashMap<>();
            params.put("transferSn",old.getTransferSn());
            params.put("inStatus",0);
            wmsTransferMapper.updateInStatus(params);

            params.put("transferSn",wmsInPlan.getTransferSn());
            params.put("inStatus",2);
            wmsTransferMapper.updateInStatus(params);
        } else if(StringUtils.isEmpty(wmsInPlan.getTransferSn())
                && StringUtils.isNotEmpty(old.getTransferSn())){
            Map<String,Object> params = new HashMap<>();
            params.put("transferSn",old.getTransferSn());
            params.put("inStatus",0);
            wmsTransferMapper.updateInStatus(params);
        } else if(StringUtils.isEmpty(old.getTransferSn())
                && StringUtils.isNotEmpty(wmsInPlan.getTransferSn())){

            Map<String,Object> params = new HashMap<>();
            params.put("transferSn",wmsInPlan.getTransferSn());
            params.put("inStatus",2);
            wmsTransferMapper.updateInStatus(params);
        }
        return 1;
    }

    /**
     * 删除入库计划对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWmsInPlanByIds(String ids)
    {
        return wmsInPlanMapper.deleteWmsInPlanByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除入库计划信息
     * 
     * @param id 入库计划ID
     * @return 结果
     */
    @Override
    public int deleteWmsInPlanById(Long id)
    {
        return wmsInPlanMapper.deleteWmsInPlanById(id);
    }
}
